import React, { Component } from 'react'
import PropTypes from 'prop-types'
import './index.css'
export default class index extends Component {
// 子组件给父组件传递值：父通过props给子传递一个函数，子调用函数传递数据
// 对接收的props的类型必要性进行限制
  static propTypes = {
    onAddToDo: PropTypes.func.isRequired
  }
  handleKeyUp = (e) => {
    if (e.keyCode !== 13) {
      return
    }
    if (e.target.value === '') {
      alert('添加的事件名称不为空！')
      return
    }
    const name = e.target.value
    console.log('子组件传递的值', name);
    this.props.onAddToDo(name)
    e.target.value = ''
  }
  render() {
    return (
      <div>
        <div className="todo-header">
            <input onKeyUp={this.handleKeyUp} type="text" placeholder="请输入你的任务名称，按回车键确认"/>
        </div>
      </div>
    )
  }
}
